package com.fand.platform.common.controller;

import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.apache.cxf.common.util.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.fand.platform.authority.dto.NavigationDto;
import com.fand.platform.authority.service.AuthorityService;
import com.fand.platform.configuration.service.ConfigurationService;
import com.fand.template.utils.TemplateUtils;

/**
 * @author yaoHT
 */
@Controller
public class AdminController {
	@Qualifier(AuthorityService.BEAN_ID)
	@Autowired(required = false)
	private AuthorityService authorityService;

	@Qualifier(ConfigurationService.BEAN_ID)
	@Autowired(required = false)
	private ConfigurationService configurationService;

	@RequestMapping(value = { "/admin/index.html", "/admin.html" }, method = RequestMethod.GET)
	public String adminIndex() {
		return "admin/index";
	}

	@RequestMapping(value = { "/admin/leftNav.html" }, method = RequestMethod.POST)
	public String adminLeftNav(final NavigationDto navigation, final Model model) {
		final List<NavigationDto> navigationList = this.authorityService.getNavigationService()
				.findNavigationByTypeCo(navigation, 2);

		if (!org.springframework.util.CollectionUtils.isEmpty(navigationList)) {
			for (NavigationDto itemNavigation : navigationList) {
				if (!org.springframework.util.CollectionUtils.isEmpty(itemNavigation.getChilds())) {
					for (NavigationDto childNavigation : itemNavigation.getChilds()) {
						this.processData(childNavigation);
					}
				}
			}
		}

		model.addAttribute("navigationList", navigationList);

		return "admin/leftNav";
	}

	@RequestMapping(value = { "/admin/topNav.html" })
	public String adminTopNav(final NavigationDto navigation, final Model model) {
		final List<NavigationDto> navigationList = this.authorityService.getNavigationService()
				.findNavigationByTypeCo(navigation, 1);

		model.addAttribute("navigationList", navigationList);

		if (StringUtils.isBlank(navigation.getId())) {
			if (!CollectionUtils.isEmpty(navigationList)) {
				final String id = navigationList.get(0).getId();

				navigation.setId(id);
			}
		}

		if (!org.springframework.util.CollectionUtils.isEmpty(navigationList)) {
			for (NavigationDto itemNavigation : navigationList) {
				this.processData(itemNavigation);
			}
		}

		model.addAttribute("id", navigation.getId());

		return "admin/topNav";
	}

	private void processData(final NavigationDto navigation) {
		if (StringUtils.isBlank(navigation.getUrl())) {
			return;
		}

		String url = navigation.getUrl();

		final Map<String, String> model = this.configurationService.getVariableService().getVariableByTypeCoForMap(
				"url");

		url = TemplateUtils.processString(url, model);

		navigation.setUrl(url);
	}
}
